package com.jsl;

import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;

import java.util.HashMap;
import java.util.Map;

public class PushLog {

	public static void addPushLog(Map<String, String> params){
		
		BaseBean baseBean=new BaseBean();
		try {
			
			String lcbt=params.get("lcbt"); //流程标题
			String lcbh=params.get("lcbh"); //流程编号
			
			String jklx=params.get("jklx"); //接口类型
			String tscs=Util.toHtml(params.get("tscs")); //推送参数
			String fhjg=Util.toHtml(params.get("fhjg")); //返回结果
			String fhxx=params.get("fhxx"); //返回信息

			String tszt = ("true").equals(params.get("tszt"))?"成功":"失败";

			String tssj=TimeUtil.getCurrentTimeString();
			
			for (int i = 0; i <=tscs.length(); i=i+3900) {
				
			    String sub = "";
			    
			    if(tscs.length()>0) {
			    	if(i+3900>tscs.length()){
				        sub = tscs.substring(i,tscs.length());
				    }else{
				        sub = tscs.substring(i,i+3900);
				    }
			    }
			    
			    RecordSet recordSet=new RecordSet();
				String sql="insert into uf_nccjkdy(lcbt,lcbh,jklx,tscs,fhjg,tszt,tssj,fhxx,modedatacreater) values(" +
						"'"+lcbt+"','"+lcbh+"','"+jklx+"','"+sub+"','"+fhjg+"','"+tszt+"','"+tssj+"','"+fhxx+"','1')";
				recordSet.execute(sql);
				
				baseBean.writeLog("[PushLog.addInterfacePushLog] sql:"+sql);
				
				addFormmodeRight("1",4,"uf_nccjkdy");
			}
			
		} catch (Exception e) {
			baseBean.writeLog("[PushLog.addInterfacePushLog] 接口日志添加失败 ERROR:"+e.toString());
			
			baseBean.writeLog(e);
		}
	
	}
	
	/**
	 * 获取日志对象
	 * @return
	 */
	public static Map<String, String> getlogParams(){
		
		Map<String, String> logParams=new HashMap<String, String>(); //日志参数
		logParams.put("jklx", ""); //接口类型
	    logParams.put("lcbt", ""); //流程标题
	    logParams.put("lcbh", ""); //数据编号
	    logParams.put("tscs", ""); //推送参数
	    logParams.put("fhjg", ""); //返回结果
	    logParams.put("fhxx", ""); //错误提示
	    
	    return logParams;
	}
	
	/**
	 * 表单建模数据增加权限
	 * @param creatorid
	 * @param modeid
	 * @param formtable
	 */
	public static int addFormmodeRight(String creatorid,int modeid,String formtable){
		
		RecordSet recordSet2=new RecordSet();
		int billid=0;
		String sql="select max(id) as maxid from "+formtable+" where modedatacreater="+creatorid;
		recordSet2.execute(sql);
		if(recordSet2.next()){
			billid=recordSet2.getInt("maxid");
		}
		
		String modedatacreatedate=TimeUtil.getCurrentDateString();
		String modedatacreatetime=TimeUtil.getOnlyCurrentTimeString();
		
		sql="update "+formtable+" set formmodeid="+modeid+",modedatacreatertype=0,modedatacreater="+creatorid+",modedatacreatedate='"+modedatacreatedate+"',modedatacreatetime='"+modedatacreatetime+"' where id="+billid;
		recordSet2.execute(sql);
		
		//构建数据权限
		ModeRightInfo ModeRightInfo = new ModeRightInfo();
		ModeRightInfo.setNewRight(true);
		ModeRightInfo.editModeDataShare(Util.getIntValue(creatorid),modeid,billid);
		
		return billid;
	}
	
	public static String getWorkflowid(String requestid){
		
		String workflowid="";
		RecordSet recordSet=new RecordSet();
		String sql="select workflowid from workflow_requestbase where requestid="+requestid;
		recordSet.execute(sql);
		if(recordSet.next()){
			workflowid=recordSet.getString("workflowid");
		}
		return workflowid;
	
	}
	
}
